﻿using System.Globalization;
using System.Management;
using System.Runtime.Versioning;
using System.Text;

namespace EWin
{

    /// <summary>
    /// 硬件信息工具
    /// </summary>
    [SupportedOSPlatform("windows")]
    public static partial class HardwareTool
    {
        /// <summary>
        /// 获取硬盘编号
        /// </summary>
        /// <returns>硬盘编号</returns>
        public static string GetHdId()
        {
            using (ManagementClass mc = new ManagementClass("Win32_DiskDrive"))
            {
                ManagementObjectCollection query = mc.GetInstances();
                StringBuilder sb = new StringBuilder();
                foreach (var obj in query)
                {
                    if (obj["Caption"] == null)
                    {
                        continue;
                    }
                    string? usbStr = obj["Caption"].ToString();
                    string? serialNo = obj["SerialNumber"].ToString();
                    if (string.IsNullOrWhiteSpace(serialNo) || string.IsNullOrWhiteSpace(usbStr) || usbStr.ToLower().Contains("usb"))
                    {
                        continue;
                    }
                    sb.Append(usbStr + ":" + serialNo + ";");
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 获取网卡mac
        /// </summary>
        /// <returns>网卡mac</returns>
        public static string GetMac()
        {
            using (ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"))
            {
                ManagementObjectCollection query = mc.GetInstances();
                StringBuilder sb = new StringBuilder();
                int i = 1;
                foreach (var obj in query)
                {
                    if (obj["MacAddress"] != null && obj["Caption"] != null)
                    {
                        string? macC = obj["Caption"].ToString();
                        string? macV = obj["MacAddress"].ToString();
                        if (string.IsNullOrWhiteSpace(macC) || string.IsNullOrWhiteSpace(macV))
                        {
                            continue;
                        }
                        if (macC.ToLower().Split(new char[] { ' ', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).ToList().Any(a => _macKeyWords.Contains(a)))
                        {
                            continue;
                        }

                        sb.Append("MAC" + i.ToString("d2") + ":" + macV.Replace(":", "-") + ";");
                        i++;
                    }
                }
                return sb.ToString();
            }
        }
        /// <summary>
        /// 获取cpu的id
        /// </summary>
        /// <returns>cpu的id</returns>
        public static string GetCPUId()
        {
            using (ManagementClass mc = new ManagementClass("Win32_Processor"))
            {
                ManagementObjectCollection query = mc.GetInstances();
                StringBuilder sb = new StringBuilder();
                int i = 1;
                foreach (var obj in query)
                {
                    sb.Append("CPU" + i.ToString("d2") + ":");
                    sb.Append(obj["Processorid"].ToString() + ";");
                    i++;
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 获取内存条编号
        /// </summary>
        /// <returns>内存条编号</returns>
        public static string GetMemoryId()
        {
            using (ManagementClass mc = new ManagementClass("Win32_PhysicalMemory"))
            {
                ManagementObjectCollection query = mc.GetInstances();
                StringBuilder sb = new StringBuilder();
                int i = 1;
                foreach (var obj in query)
                {
                    sb.Append("MEMORY" + i.ToString("d2") + ":");
                    sb.Append(obj["PartNumber"].ToString() + ";");
                    i++;
                }
                return sb.ToString();
            }
        }

        /// <summary>
        /// 获取操作系统信息
        /// </summary>
        /// <returns>操作系统信息</returns>
        public static string GetOsInfo()
        {
            StringBuilder sr = new StringBuilder();
            sr.AppendFormat("MachineName:{0};OSVersion:{1};SystemDirectory:{2};Language:{3};.NETVersion:{4};CurrentDirectory:{5};CurrentUser:{6};",
                Environment.MachineName,
                Environment.OSVersion,
                Environment.SystemDirectory,
                CultureInfo.InstalledUICulture.EnglishName,
                Environment.Version,
                AppDomain.CurrentDomain.BaseDirectory,
                Environment.UserName);
            return sr.ToString();
        }

        /// <summary>
        /// mac过滤的核心词汇
        /// </summary>
        private static readonly List<string> _macKeyWords = new List<string>() { "microsoft", "virtual", "vpn", "wan", "bluetooth" };
    }
}